package cn.jn.lhm.teacher.handler;

import cn.jn.lhm.teacher.constant.EduConstant;
import cn.jn.lhm.teacher.entity.EduCourse;
import cn.jn.lhm.teacher.entity.vo.CourseInfoVo;
import cn.jn.lhm.teacher.entity.vo.CourseQueryCondition;
import cn.jn.lhm.teacher.service.EduCourseService;
import cn.jn.lhm.utils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/6/2 - 21:27
 */
@RestController
@RequestMapping("/course")
public class EduCourseHandler {

    @Resource
    private EduCourseService eduCourseService;


    @GetMapping("/getAll")
    public R getAllCourse(){
        return R.ok().data("data", eduCourseService.getAllCourse());
    }

    @GetMapping("/get/{id}")
    public R getCourseInfo(@PathVariable("id")String id){
        CourseInfoVo courseInfoVo =  eduCourseService.getCourseInfo(id);
        return R.ok().data("data", courseInfoVo);
    }

    @GetMapping("/get/publish/{id}")
    public R getPublishInfo(@PathVariable("id")String id){
        return R.ok().data("data", eduCourseService.getPublishCourseInfoByCourseId(id));
    }

    @PostMapping("/update")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @PostMapping("/publish/{id}")
    public R publishCourse(@PathVariable("id")String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus(EduConstant.PUBLISH_STATUS_ALREADY);
        eduCourseService.changeCoursePublish(eduCourse);
        return R.ok();
    }

    @PostMapping("/add")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfo){
        //为了后面的继续添加，需要返回添加一行的课程id
        String id = eduCourseService.saveCourseInfo(courseInfo);
        return R.ok().data("courseId",id);
    }

    @PostMapping("/getCourse/{current}/{limit}")
    public R getCourseByCondition(@RequestBody CourseQueryCondition queryCondition,
                                  @PathVariable("current")Long current,
                                  @PathVariable("limit")Long limit){
        Page<EduCourse> page = new Page<>(current, limit);
        return eduCourseService.getCourseByCondition(queryCondition, page);
    }

    @DeleteMapping("/delete/{id}")
    public R deleteCourseById(@PathVariable("id")String id){
        eduCourseService.removeCourseAllInfo(id);
        return R.ok();
    }
}
